package com.Travit;

import java.io.IOException;
import java.util.Hashtable;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class UserProfileManage extends Activity{

	String username;
	String userpass;
	
	ImageButton back;
	
	private static String urlRequest = "http://project492.engr.sjsu.edu/travIT.php";
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Log.v("getting user", "try");
		Bundle extras = getIntent().getExtras();
		username = extras.getString("inusername");
		userpass = extras.getString("inuserpass");
		Log.v("getting user", username);
		Log.v("getting user", userpass);
		setContentView(R.layout.profile);
		//back = (ImageButton) findViewById(R.id.goBackButton);
		
		EditText profileEmail = (EditText) findViewById(R.id.showProfileEmail);
		TextView profileUsername = (TextView) findViewById(R.id.showProfileusername);
		Log.v("username:", username);
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(urlRequest + "?username="
				+ username + "&userpass="
				+ userpass);
		Log.v("see", "see");
		try {
			HttpResponse response = client.execute(request);
			HttpEntity entity = response.getEntity();
			String responseResult = EntityUtils.toString(entity);
			Log.v("PROFILE:", responseResult);
			String[] table = responseResult.split(",");
			Hashtable<String, String> tableHashtable = new Hashtable();
			for (int i = 0; i < table.length; i++) {
				Log.v(table[i], table[++i]);
				tableHashtable.put(table[i], table[++i]);
			}
			System.out.println("check:");

			profileUsername.setText(tableHashtable.get("username"));
			// profileUsername.setEnabled(false);
			profileEmail.setText(tableHashtable.get("email"));
			// profileUsername.setClickable(false);
			TextView userType = (TextView) findViewById(R.id.userIDProfile);
			userType.setText(tableHashtable.get("usertype"));
//			homepageImageButton = (ImageButton) findViewById(R.id.homepageButton);
//			gobackImageButton = (ImageButton) findViewById(R.id.goBackButton);
//			// homepageImageButton.setImageResource(R.drawable.homepage);
//			// gobackImageButton.setImageResource(R.drawable.go_back);
//			homepageImageButton.setOnClickListener(this);
//			gobackImageButton.setOnClickListener(this);
//			deleteProfileUserButton = (Button) findViewById(R.id.deleteProfileUser);
//			updateProfileUserButton = (Button) findViewById(R.id.updateProfileUser);
//			deleteProfileUserButton.setOnClickListener(this);
//			updateProfileUserButton.setOnClickListener(this);
//			logoutImageButton = (ImageButton) findViewById(R.id.logout);
//			logoutImageButton.setOnClickListener(this);

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setContentView(R.layout.homepage);
				// TODO Auto-generated method stub
//				Intent newIntent = new Intent(v.getContext(), TravITActivity.class);
//				newIntent.putExtra("inusername", username);
//				newIntent.putExtra("inuserpass", userpass);
//				startActivity(newIntent);
			}
		});
	}
}
